package com.starhub.domain.knowledge.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.starhub.application.collection.dto.DataDefinitionLinkDto;
import com.starhub.common.bean.ResultResp;
import com.starhub.common.security.util.SecurityUtils;
import com.starhub.common.sys.user.LoginUser;
import com.starhub.domain.knowledge.dto.KnowledgeBaseDto;
import com.starhub.domain.knowledge.dto.KnowledgeCollectionDto;
import com.starhub.domain.knowledge.service.KnowledgeSettingService;
import com.starhub.domain.knowledge.vo.KnowledgeBaseVo;
import com.starhub.domain.knowledge.vo.KnowledgeCollectionVo;
import com.starhub.utils.Util;

import lombok.extern.slf4j.Slf4j;

/**
 * 知识库设置接口
 * @version: V1.0
 * @author: liuhf
 * @time 2025-06-06
 */
@Slf4j
@RestController
@RequestMapping("/api/starhub/setting/knowledge")
public class KnowledgeSettingController {

    @Autowired
    private KnowledgeSettingService knowledgeSettingService;

    /**
     * 创建知识库
     */
    @PostMapping("/base/create")
    public ResultResp<Boolean> createKnowledgeBase(@RequestBody KnowledgeBaseDto knowledgeBase) {
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            knowledgeBase.setCreatorId(Util.getLongValue(user.getUserId()));
            boolean result = knowledgeSettingService.createKnowledgeBase(knowledgeBase);
            return ResultResp.success(result);
        } catch (Exception e) {
            return ResultResp.fail(e.getMessage());
        }
    }

    /**
     * 更新知识库
     */
    @PostMapping("/base/update")
    public ResultResp<Boolean> updateKnowledgeBase(@RequestBody KnowledgeBaseDto knowledgeBase) {
        try {
            boolean result = knowledgeSettingService.updateKnowledgeBase(knowledgeBase);
            return ResultResp.success(result);
        } catch (Exception e) {
            return ResultResp.fail(e.getMessage());
        }
    }

    /**
     * 删除知识库
     */
    @PostMapping("/base/delete/{id}")
    public ResultResp<Boolean> deleteKnowledgeBase(@PathVariable Long id) {
        try {
            boolean result = knowledgeSettingService.deleteKnowledgeBase(id);
            return ResultResp.success(result);
        } catch (Exception e) {
            return ResultResp.fail(e.getMessage());
        }
    }

    /**
     * 创建知识库表
     */
    @PostMapping("/collection/create")
    public ResultResp<Boolean> createKnowledgeCollection(@RequestBody KnowledgeCollectionDto knowledgeCollection) {
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            knowledgeCollection.setCreatorId(Util.getLongValue(user.getUserId()));
            boolean result = knowledgeSettingService.createKnowledgeCollection(knowledgeCollection);
            return ResultResp.success(result);
        } catch (Exception e) {
            log.error("创建知识库表失败", e);
            return ResultResp.fail(e.getMessage());
        }
    }

    /**
     * 更新知识库表
     */
    @PostMapping("/collection/update")
    public ResultResp<Boolean> updateKnowledgeCollection(@RequestBody KnowledgeCollectionDto knowledgeCollection) {
        try {
            boolean result = knowledgeSettingService.updateKnowledgeCollection(knowledgeCollection);
            return ResultResp.success(result);
        } catch (Exception e) {
            return ResultResp.fail(e.getMessage());
        }
    }

    /**
     * 删除知识库表
     */
    @PostMapping("/collection/delete/{id}")
    public ResultResp<Boolean> deleteKnowledgeCollection(@PathVariable Long id) {
        try {
            boolean result = knowledgeSettingService.deleteKnowledgeCollection(id);
            return ResultResp.success(result);
        } catch (Exception e) {
            return ResultResp.fail(e.getMessage());
        }
    }

    /**
     * 获取知识库信息
     */
    @GetMapping("/base/get/{id}")
    public ResultResp<KnowledgeBaseVo> getKnowledgeBaseById(@PathVariable Long id) {
        try {
            KnowledgeBaseVo knowledgeBase = knowledgeSettingService.getKnowledgeBaseById(id);
            return ResultResp.success(knowledgeBase);
        } catch (Exception e) {
            return ResultResp.fail(e.getMessage());
        }
    }

    /**
     * 获取知识库表信息
     */
    @GetMapping("/collection/get/{id}")
    public ResultResp<KnowledgeCollectionVo> getKnowledgeCollectionById(@PathVariable Long id) {
        try {
            KnowledgeCollectionVo collection = knowledgeSettingService.getKnowledgeCollectionById(id);
            return ResultResp.success(collection);
        } catch (Exception e) {
            return ResultResp.fail(e.getMessage());
        }
    }

    /**
     * 添加主数据表和元数据表的关联
     */
    @PostMapping("/link/add")
    public ResultResp<Boolean> addLink(@RequestBody DataDefinitionLinkDto dataDefinitionLinkDto) {
        return ResultResp.success(knowledgeSettingService.addLink(dataDefinitionLinkDto));
    }

    /**
     * 移除主数据表和元数据表的关联
     */
    @PostMapping("/link/remove")
    public ResultResp<Boolean> removeLink(@RequestBody DataDefinitionLinkDto dataDefinitionLinkDto) {
        return ResultResp.success(knowledgeSettingService.removeLink(dataDefinitionLinkDto));
    }

    /**
     * 获取元数据关联设置
     */
    @GetMapping("/link/get/{mainCollection}")
    public ResultResp<List<KnowledgeCollectionVo>> getLinks(@PathVariable Long mainCollection) {
        return ResultResp.success(knowledgeSettingService.getLinks(mainCollection));
    }
}
